import {Component, EventEmitter, Output, Input} from '@angular/core';

@Component({
    selector: 'client-list-option',
    templateUrl: 'client-list-option.html'
})
export class ClientListOptionComponent {
    @Output() itemChanged;
    @Input()
    pageKind = "";
    options = [
        {
            t: '近一周',
            isCurrent: false,
            v: 7
        },
        {
            t: '近一月',
            isCurrent: false,
            v: 30
        },
        {
            t: '近两月',
            isCurrent: false,
            v: 60
        },
        {
            t: '近三月',
            isCurrent: true,
            v: 90
        }
    ];

    constructor() {
        this.itemChanged = new EventEmitter();
    }

    ngOnInit() {
        if (this.pageKind) {
            this.options = [
                {
                    t: '近一周',
                    isCurrent: false,
                    v: 7
                },
                {
                    t: '近两周',
                    isCurrent: false,
                    v: 14
                },
                {
                    t: '近一月',
                    isCurrent: false,
                    v: 30
                },
                {
                    t: '近两月',
                    isCurrent: true,
                    v: 60
                }
            ];
        }
    }

    public getCurrentItem() {
        let current = null;
        let data = this.options.filter(o => o.isCurrent);
        if (data && data.length) {
            current = data[0];
        }
        return current;
    }

    emitChange() {
        let data = this.options.filter(o => o.isCurrent);
        if (data && data.length) {
            this.itemChanged.emit(data[0]);
        }
    }

    optionClicked(e, item) {
        this.options.forEach(i => {
            i.isCurrent = i.t === item.t;
        });
        this.emitChange();
    }

}
